package com.jctrip.hotel.route.service;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jctrip.hotel.domain.dao.HtHotelMthdomInfoDao;
import com.jctrip.hotel.domain.dao.HtHotelStdInfoDao;
import com.jctrip.hotel.domain.entity.HtHotelMthdomInfo;
import com.jctrip.hotel.domain.entity.HtHotelStdInfo;
import com.jctrip.hotel.domain.entity.HtPricePolicy;
import com.jctrip.hotel.route.model.param.GetHotelIdListParam;
import com.jctrip.hotel.route.model.result.GetHotelIdListResult;
import lombok.Builder;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 获取酒店ID列表接口-服务
 */
@Service
@Slf4j
public class GetHotelIdListService extends RouteService {

    @Autowired
    private HtHotelStdInfoDao htHotelStdInfoDao;
    @Autowired
    private HtHotelMthdomInfoDao htHotelMthdomInfoDao;

    public GetHotelIdListResult execute(GetHotelIdListParam param) {
        // 价格政策配置
        List<HtPricePolicy> htPricePolicies = getPricePolicies();
        List<String> sourceCodes = htPricePolicies.stream()
                .map(HtPricePolicy::getSourceCode)
                .collect(Collectors.toList());
        Page<HtHotelStdInfo> pageParam = Page.of(param.getPageNum(), param.getPageSize());
        LambdaQueryWrapper<HtHotelStdInfo> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.in(HtHotelStdInfo::getSourceCode, sourceCodes);
        // 酒店分页数据
        Page<HtHotelStdInfo> pageResult = htHotelStdInfoDao.selectPage(pageParam, queryWrapper);
        List<String> hotelIds = pageResult.getRecords().stream()
                .map(HtHotelStdInfo::getHotelId)
                .collect(Collectors.toList());
        if (sourceCodes.contains("MTHDOM")) {
            long pages = 0L;
            long total = pageResult.getTotal();
            if (param.getPageNum()*param.getPageSize() <= pageParam.getTotal()) {
                Page<HtHotelMthdomInfo> mthdomInfoParam = Page.of(1L,param.getPageSize());
                Page<HtHotelMthdomInfo> mthdomInfoPageResult = htHotelMthdomInfoDao.selectPage(mthdomInfoParam,null);
                pages = (pageResult.getTotal()+mthdomInfoPageResult.getTotal())%param.getPageSize() == 0L?
                            (pageResult.getTotal()+mthdomInfoPageResult.getTotal())/param.getPageSize() : (pageResult.getTotal()+mthdomInfoPageResult.getTotal())/param.getPageSize() +1;
                total += mthdomInfoPageResult.getTotal();

            } else if (param.getPageNum()*param.getPageSize() > pageParam.getTotal()) {
                long remain = pageParam.getTotal() % param.getPageSize();
                if (remain == 0L){
                    long mtPageNum = param.getPageNum() - pageParam.getPages();
                    //获取美团酒店id
                    MthdomHotelids mthdomHotelids = getMthdomHotelids(mtPageNum, param.getPageSize());
                    hotelIds.addAll(mthdomHotelids.hotelIds);
                    pages = (pageResult.getTotal()+mthdomHotelids.getTotal())%param.getPageSize() == 0L?
                                (pageResult.getTotal()+mthdomHotelids.getTotal())/param.getPageSize() : (pageResult.getTotal()+mthdomHotelids.getTotal())/param.getPageSize() +1;
                    total += mthdomHotelids.getTotal();
                } else {
                    if (param.getPageNum()  == pageParam.getPages()){
                        //获取美团酒店id
                        MthdomHotelids mthdomHotelids = getMthdomHotelids(1L,remain);
                        hotelIds.addAll(mthdomHotelids.hotelIds);
                        pages = (pageResult.getTotal()+mthdomHotelids.getTotal())%param.getPageSize() == 0L?
                                    (pageResult.getTotal()+mthdomHotelids.getTotal())/param.getPageSize() : (pageResult.getTotal()+mthdomHotelids.getTotal())/param.getPageSize() +1;
                        total += mthdomHotelids.getTotal();
                    } else {
                        Integer mtNum1 = Math.toIntExact(param.getPageNum() - pageParam.getPages());
                        Integer mtNum2 = Math.toIntExact(param.getPageNum() - pageParam.getPages())+1;
                        //获取美团酒店id
                        MthdomHotelids mthdomHotelids1 = getMthdomHotelids(mtNum1,param.getPageSize());
                        MthdomHotelids mthdomHotelids2 = getMthdomHotelids(mtNum2,param.getPageSize());
                        //移除首尾多余内容
                        int count = (int) (param.getPageSize()-remain);
                        List<String> sublist1 = mthdomHotelids1.hotelIds.subList((int) remain,param.getPageSize());
                        List<String> sublist2 = mthdomHotelids2.hotelIds.subList(0,count);
                        hotelIds.addAll(sublist1);
                        hotelIds.addAll(sublist2);
                        pages = (pageResult.getTotal()+mthdomHotelids1.getTotal())%param.getPageSize() == 0L? (pageResult.getTotal()+mthdomHotelids1.getTotal())/param.getPageSize() :(pageResult.getTotal()+mthdomHotelids1.getTotal())/param.getPageSize() +1;
                        total += mthdomHotelids1.getTotal();
                    }
                }
            }
            return GetHotelIdListResult.builder()
                    .pageNum(param.getPageNum())
                    .pageSize(param.getPageSize())
                    .hotelIds(hotelIds)
                    .total(total)
                    .pages(pages)
                    .build();
        }
        return GetHotelIdListResult.builder()
                .pageNum(param.getPageNum())
                .pageSize(param.getPageSize())
                .total(pageResult.getTotal())
                .pages(pageResult.getPages())
                .hotelIds(hotelIds)
                .build();
    }

    private  MthdomHotelids getMthdomHotelids (long pageNum,long pageSize){
        Page<HtHotelMthdomInfo> mthdomInfoParam = Page.of(pageNum,pageSize);
        Page<HtHotelMthdomInfo> mthdomInfoPageResult = htHotelMthdomInfoDao.selectPage(mthdomInfoParam,null);
        return MthdomHotelids.builder()
                .hotelIds(mthdomInfoPageResult.getRecords().stream()
                        .map(HtHotelMthdomInfo::getHotelId)
                        .collect(Collectors.toList()))
                .pages(mthdomInfoPageResult.getPages())
                .total(mthdomInfoPageResult.getTotal())
                .build();

    }
    @Data
    @Builder
    public static class MthdomHotelids {
        private List<String> hotelIds;
        private Long pages;
        private Long total;
    }

}
